<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>新增单位</title>
    <link rel="stylesheet" type="text/css" href="./css/main.css" />
    <link rel="stylesheet" type="text/css" href="./layui/css/layui.css" />
    <link rel="stylesheet" type="text/css" href="./css/layuiAdminTheme.css" />
    <style type="text/css">

    </style>
</head>
<body>
<script src="./js/main.js" charset="utf-8"></script>
<!--<script src="https://cdn.jsdelivr.net/npm/jquery" charset="utf-8"></script>-->
<script src="./layui/layui.js" charset="utf-8"></script>
<script src="./js/jQuery.main.js"></script>
<form class="layui-form" action="" lay-filter="data">

    <div class="layui-form-item">
        <div class="layui-block">
            <label class="layui-form-label">机器型号：</label>
            <div class="layui-input-inline " style="width: 14em">
                <select name="machineModel" id="machineModel" lay-verify="required" lay-filter="machineModel">
                </select></div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-block">
            <label class="layui-form-label">所属部门：</label>
            <div class="layui-input-inline " style="width: 14em">
            <select name="deptName" id="deptName" lay-verify="required" lay-filter="deptName">
            </select></div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-block">
            <label class="layui-form-label">机械编号：</label>
            <input type="text" name="machineNum" lay-verify="required" autocomplete="" class="layui-input" style="width: 14em;"
                   placeholder="请输入机械编号">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label"></label>
        <input type="text" name="machine" style="display: none;" value="0">
        <button type="button" class="layui-btn" lay-submit=''>添加机器</button>
    </div>
</form>

<script type="text/javascript">
    var form = null;
    let isAd=null;
    let thisDept=null;
    var laydate = null;

    layui.use(['form', 'laydate'], function() {
        form = layui.form;
        laydate = layui.laydate;

        $.ajax({
            url: url + '/sys-dept/listDept',
            method: 'POST',
            async : false,
            contentType: 'application/json',
            success: function(res) {
                //显示前先判断是否为管理员
                $.ajax({
                    url: '/sys-user/getAdmin/'+sessionStorage.getItem('userId'),
                    method: 'GET',
                    async : false,
                    contentType: 'application/json',
                    success: function (res) {
                        if (res.code===200){
                            isAd=1;
                        }
                        else if (res.code===400){
                            isAd=0;
                            thisDept=res.data.dept;
                        }
                    }
                })
                //按照是否为管理员显示下拉框
                if (res.code == 200) {
                    for (var i = 0; i < res.data.length; i++) {
                        if(isAd===1){
                            $("#deptName").append("<option value='" + res.data[i].id + "'>" + res.data[i].dept +"</option>");
                        }else if (isAd===0&&res.data[i].dept===thisDept){
                            $("#deptName").append("<option selected='selected' value='" + res.data[i].id + "'>" + res.data[i].dept +"</option>").attr('disabled','disabled');
                        }
                    }
                    form.render('select');
                }
            }
        })
        $.ajax({
            url: url + '/machine-info/listMachineType/'+$('#deptName').val(),
            method: 'GET',
            async : false,
            contentType: 'application/json',
            success: function(res) {
                //显示下拉框
                if (res.code == 200) {
                    for (var i = 0; i < res.data.length; i++) {
                        $("#machineModel").append("<option value='" + res.data[i].id + "'>" + res.data[i].machineModel +"</option>");
                    }
                    form.render('select');
                }
            }
        })
        //下拉框联动
        form.on('select(deptName)', function(data) {
            $("#machineModel").empty()
            $.ajax({
                url: url + '/machine-info/listMachineType/'+$('#deptName').val(),
                method: 'GET',
                async : false,
                contentType: 'application/json',
                success: function(res) {
                    //显示下拉框
                    if (res.code == 200) {
                        for (var i = 0; i < res.data.length; i++) {
                            $("#machineModel").append("<option value='" + res.data[i].id + "'>" + res.data[i].machineModel +"</option>");
                        }
                        form.render('select');
                    }
                }
            })
        });
        form.on('submit', function() {
            $.ajax({
                url: '/sys-user/getAdminLevel/'+sessionStorage.getItem('userId'),
                method: 'GET',
                contentType: 'application/json',
                success: function (res) {
                    if (res.code === 200) {
                        var data = form.val('data');
                        data.userId=sessionStorage.getItem('userId');
                        data.machineInfoName=$('#machineModel option:selected').text();
                        data.deptId=$('#deptName').val();
                        let confirm1=layer.confirm('确定新增机器吗？', {
                            btn: ['确定','取消']
                        },function () {
                            $.ajax({
                                url: url + '/all-machine/save',
                                method: 'POST',
                                contentType: 'application/json',
                                data: JSON.stringify(data),
                                success: function (res) {
                                    parent.layer.alert(res.msg);
                                    if (res.code == 200) parent.switchNav(0);
                                }
                            })
                            layer.close(confirm1);
                        },function (){
                            layer.close(confirm1);
                        })
                    }else {
                        layer.msg("权限不足");
                    }
                }
            })
        })
    })
</script>
</body>
</html>
